package Main;

import java.util.*;
import java.lang.*;

import Include.*;
import Common.*;

public class Translator {
    private Map<String, List<TACLine>> TACFile_;
    private Scope Global_Scope_;
    private List<ASMSection> asmFile = new ArrayList<>();

    public Translator(Map<String, List<TACLine>> TACFile_, Scope Global_Scope_) {
        // check
        // System.out.println("Entering Translator constructor");
        if (TACFile_ == null) {
            throw new IllegalArgumentException("TACFile_ cannot be null");
        }

        this.TACFile_ = TACFile_;
        this.Global_Scope_ = Global_Scope_;

        // System.out.println("Exiting Translator constructor");
    }

    public void Translate() {
        dataTranslate();
        textTranslate();
    }

    private void dataTranslate() {
        ASMSection asmSection = new ASMSection();
        asmSection.name = "data";

        ASMBlock ASMBlock_ = new ASMBlock();
        ASMBlock_.name = "";
        Map<String, List<TACLine>> global_ = new HashMap<>();
        global_.put("global", TACFile_.get("global"));
        if (global_ != null) {
            for (TACLine tacLine : global_.get("global")) {
                String ASMLine_ = "";
                if (tacLine.op == TacOp.ASSIGN) {
                    ASMLine_ = tacLine.dst.value + " equ " + tacLine.src1.value;
                    ASMBlock_.asmLines.add(ASMLine_);
                } else {
                    System.out.println("global op error: " + tacLine.op);
                }
            }
            asmSection.asmBlocks.add(ASMBlock_);
            asmFile.add(asmSection);
        }
    }

    private void textTranslate() {
        ASMSection ASMSection_ = new ASMSection();
        ASMSection_.name = "text";

        ASMBlock ASMBlock_ = new ASMBlock();
        ASMBlock_.name = "";
        ASMBlock_.asmLines.add("global _start");
        ASMBlock_.asmLines.add("extern myPrint");
        ASMSection_.asmBlocks.add(ASMBlock_);

        for (Map.Entry<String, List<TACLine>> entry : TACFile_.entrySet()) {
            String key = entry.getKey();
            if (key.equals("global") || key.equals("myPrint") || entry.getValue().isEmpty()) {
                continue;
            }
            System.out.println("开始翻译函数");
            SymbolManager SymbolManager_ = new SymbolManager(Global_Scope_, key);
            ASMBlock_ = BlockTranslator.BlockTranslate(SymbolManager_, entry.getValue());
            ASMSection_.asmBlocks.add(ASMBlock_);
        }

        asmFile.add(ASMSection_);
    }

    public void OutputFile(String filename) {
        try (java.io.PrintWriter outfile = new java.io.PrintWriter(filename)) {
            for (ASMSection asmSection : asmFile) {
                if (!asmSection.name.isEmpty()) {
                    outfile.println(asmSection.name + ":");
                }
                for (ASMBlock asmBlock : asmSection.asmBlocks) {
                    if (!asmBlock.name.isEmpty()) {
                        outfile.println(asmBlock.name + ":");
                    }
                    for (String asmLine : asmBlock.asmLines) {
                        outfile.println(asmLine);
                    }
                    outfile.println();
                }
                outfile.println();
            }
        } catch (java.io.IOException e) {
            System.err.println("Error writing output file: " + e.getMessage());
        }
    }
}